package com.gitee.l0km.codegen.decorator.plugin;

import java.util.List;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;

import com.gitee.l0km.codegen.base.CodeGenUtils;
import com.gitee.l0km.codegen.base.plugin.BasePlugin;
import com.gitee.l0km.codegen.decorator.DecoratorConfiguration;
import com.gitee.l0km.codegen.decorator.SpringDecoratorConfiguration;
import com.gitee.l0km.codegen.decorator.SpringDecoratorGenerator;

/**
 * {@link SpringDecoratorGenerator}生成器运行插件
 * @author guyadong
 *
 */
@Mojo(name = "spring",defaultPhase=LifecyclePhase.COMPILE,requiresDependencyResolution=ResolutionScope.COMPILE)
public class SpringDecoratorPlugin extends BasePlugin{
	/**
	 * implement class name
	 */
	@Parameter(required = true)
	private String implClass = null;
	/**
	 * The target package name
	 */
	@Parameter(alias="package",required =true)
	private String pkg ;
	/**
	 * path list of source separated by semicolon
	 */
	@Parameter()
	private String sourcePath;
	/**
	 * exclude annotation Class names which will be skip for generation,separated with [,]without blank space
	 */
	@Parameter()
	private String excludeAnnotations;

	/**
	 * Specifies the paths where javadoc will look for referenced classes (.class files),
	 * Ignored since 3.3.0
	 */
	@Parameter()
	private String classPath;
	/**
	 * use Response family class in common-java(com.gitee.l0km:common-base:2.2.0 or above) in spring.controller.class.vm 
	 */
	@Parameter(property="use.common.response",defaultValue="false")
	private boolean useCommonResponse = false;
	/**
	 * prefix of path for service port,default is '/${implClass_name}/' 
	 */
	@Parameter()
	private String portPrefix;
	/**
	 * add annotation com.gitee.l0km.aocache.annotations.AoTimecost for every method
	 */
	@Parameter()
	private boolean timecost = false;
	/**
	 * add annotation com.gitee.l0km.codegen.annotations.DelegateMethod for delegate() method
	 */
	@Parameter()
	private boolean withDelegateMethod = true;
	public SpringDecoratorPlugin() {
	}

	@Override
	protected String getTemplateFolder() {
		return SpringDecoratorConfiguration.SPRING_FOLDER;
	}

	@Override
	public void execute() throws MojoExecutionException, MojoFailureException {
		SpringDecoratorGenerator.main(makeArgs().toArray(new String[0]));
	}

	@Override
	protected List<String> makeArgs() {
		List<String> args = super.makeArgs();
		args.add("--" + SpringDecoratorConfiguration.IMPL_CLASS_OPTION_LONG);
		args.add(implClass);
		args.add("--" + SpringDecoratorConfiguration.PACKAGE_OPTION_LONG);
		args.add(pkg);
		if(null != sourcePath){
			args.add("--" + SpringDecoratorConfiguration.SOURCE_PREFIX_OPTION_LONG);
			args.add(sourcePath);
		}
		if(null != excludeAnnotations){
			args.add("--" + SpringDecoratorConfiguration.EXCLUDE_ANN_OPTION_LONG);
			args.add(excludeAnnotations);
		}
		classPath = buildClasspath();
		args.add("--" + DecoratorConfiguration.CLASS_PATH_OPTION_LONG);
		args.add(classPath);
		/** 初始项目ClassLoader */
		CodeGenUtils.initProjectClassLoader(classPath, getClass().getClassLoader());
		if(useCommonResponse){
			args.add("--" + SpringDecoratorConfiguration.USE_COMMON_RESPONSE_OPTION_LONG);
		}
		if(null != portPrefix){
			args.add("--" + SpringDecoratorConfiguration.PORT_PREFIX_OPTION_LONG);
			args.add(portPrefix);
		}
		if(timecost){
			args.add("--" + SpringDecoratorConfiguration.TIMECOST_OPTION_LONG);
		}
		if(withDelegateMethod){
			args.add("--" + SpringDecoratorConfiguration.WITH_DELEGATE_METHOD_OPTION_LONG);
		}
		return args;
	}

}
